layui.use(['form', 'jquery'], function () {
  var form = layui.form
    , layer = layui.layer
    , $ = layui.$;

  //自定义验证规则
  form.verify({
    pass: [
      /^[\S]{6,12}$/
      , '密码必须6到12位，且不能出现空格'
    ]
    , re_pass: function (value, item) {
      if (value != $('input[name="password"]').val()) {
        return '两次密码不一致'
      }
    }
    , verify: [
      /^[\S]{4}$/
      , '验证码必须4位'
    ]
  });

  // 生成uuid
  function generateUUID() {
    let d = new Date().getTime(); // 获取当前时间
    let uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
      let r = (d + Math.random() * 16) % 16 | 0;
      d = Math.floor(d / 16);
      return (c == 'x' ? r : (r & 0x3 | 0x8)).toString(16);
    });
    return uuid;
  }


  // console.log(generateUUID())
  // 生成img标签
  $('#verification_image_box').html('<img alt="请稍后重试" id="verification_image">');
// 生成隐藏input存储uuid
  $('#uuid_box').html('<input type="hidden" id="uuid" name="uuid">')

  // 获取图片验证码
  function get_verification_image() {
    let uuid = generateUUID();
    $('#verification_image').attr("src", "/image/verification/" + uuid + "/");
    $('#uuid').attr("value", uuid)
  }

  get_verification_image();
  // 实现点击刷新
  $('#verification_image').click(get_verification_image);
  // 实现三分钟图片验证码失效后页面自动刷新验证码
  setInterval(get_verification_image, 1000 * 60 * 3);


  // 监听提交
  // form.on('submit(login)', function (data) {
  //   console.log(data.field);
  //   // data.field['uuid'] = uuid;
  //   console.log(data.field);
  //   $.post("/login/", data.field);
  // });
});